package oj0329.oj0329_02;

//import javax.xml.soap.Node;

import java.util.Scanner;

class Node{
    int data;
    Node next;

    public Node(int data) {
        this.data = data;
        this.next = null;
    }
}


class LinkStack{
    private Node top;

    public LinkStack(){
        top = null;
    }

    public void push(int x){
//        创建一个新的节点
        Node n = new Node(x);
        n.next = top;   //栈顶
        top = n;
    }

    public int pop(){
        if (isEmpty()){
            System.out.println("Stack is Empty");
            return -1;
        }
        int x = top.data;   //栈顶的元素先保存
        top = top.next;     //替换
        return x;
    }

    public int getTop(){
        if (isEmpty()){
            System.out.println("Stack is Empty");
            return -1;
        }
        return top.data;
    }

    public boolean isEmpty(){
        return top == null;
    }
}


public class Main {
    public static void main(String[] args) {
        LinkStack stack = new LinkStack();
        Scanner scanner = new Scanner(System.in);
        int row = scanner.nextInt();  //操作行数
        int res;
        for (int i = 0; i < row; i++) {
            int a = scanner.nextInt();
            if (a > 3 || a < 1){
                break;
            }
            switch (a){
                case 1:
                    int b = scanner.nextInt();
                    stack.push(b);
                    break;
                case 2:
                    stack.pop();
                    break;
                case 3:
                    res = stack.getTop();
                    if (res != -1){
                        System.out.println(res);
                    }
                    break;
                default:
                    break;
            }
        }
        scanner.close();
    }
}
